﻿Public Class TaskServiceImpl
    Inherits PersistenceServiceImpl(Of Task, TaskManager)
    Implements TaskService

    Sub New()
        MyBase.New(New TaskManagerImpl())
    End Sub

    Protected Overrides Sub beforeDelete(ByRef entity As Task)
        Context.ZEITRAPPORT_SERVICE.deleteByTask(entity)
    End Sub

    Protected Overrides Sub beforeSave(ByRef entity As Task)
        For Each zeitrapport As Zeitrapport In entity.zeitrapporte
            Context.ZEITRAPPORT_SERVICE.save(zeitrapport)
        Next
    End Sub

    Protected Overrides Sub fillUp(ByRef entity As Task)
        entity.zeitrapporte = Context.ZEITRAPPORT_SERVICE.findByTask(entity)
        For Each zeitrapport As Zeitrapport In entity.zeitrapporte
            zeitrapport.task = entity
        Next
    End Sub
    
    Public Sub deleteByProjekt(ByRef projekt As Projekt) Implements TaskService.deleteByProjekt
        addFireBlock()
        Connection.open()
        For Each task As Task In projekt.tasks
            beforeDelete(task)
        Next
        manager.deleteByProjekt(projekt)
        Connection.close()
        removeFireBlock()
        For Each task As Task In projekt.tasks
            fireDeleted(task)
        Next
    End Sub

    Public Function findByProjekt(ByRef projekt As Projekt) As System.Collections.Generic.List(Of Task) Implements TaskService.findByProjekt
        Connection.open()
        Dim results As List(Of Task) = manager.findByProjekt(projekt)
        For Each task As Task In results
            fillUp(task)
        Next
        Connection.close()
        Return results
    End Function
End Class
